import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:osg/controllers/custom_widget.dart';
import 'package:osg/controllers/http.dart';

class Announcement extends StatelessWidget {
  RxList announcementData = [].obs;
  RxList categoryData = [].obs;
  RxInt index = 0.obs;

  getAnnouncement(id) {
    Get.find<HTTPS>()
        .getData('/announcement', {'category_id': '$id'}).then((res) {
      announcementData.value = res;
    });
  }

  getCategory() {
    Get.find<HTTPS>().getData('/announcement/category', {}).then((res) {
      categoryData.value = res;
      getAnnouncement(0);
    });
  }

  @override
  Widget build(BuildContext context) {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      getCategory();
    });

    return Scaffold(
      appBar: AppBar(
          backgroundColor: Color(0xffffffff),
          title: Text('平台公告'),
          centerTitle: true),
      body: ListView(
        children: [
          SizedBox(
            height: 30,
            child: Obx(
              () => ListView(
                scrollDirection: Axis.horizontal,
                children: [
                  
                  for (var i = 0; i < categoryData.length; i++)
                    GestureDetector(
                      onTap: () {
                        index.value = i;
                        getAnnouncement(categoryData[i]['id']);
                      },
                      child: Container(
                        margin: EdgeInsets.only(left: 10),
                        alignment: Alignment.center,
                        padding: EdgeInsets.symmetric(horizontal: 10),
                        decoration: BoxDecoration(
                            color: index.value == i
                                ? Color(0xff0F172B)
                                : Color(0XFFF8F9FD),
                            borderRadius: BorderRadius.circular(30)),
                        child: Text(
                          '${categoryData[i]['title']}',
                          style: TextStyle(
                              fontSize: 14,
                              color: index.value == i
                                  ? Color(0xffffffff)
                                  : Color(0XFF0F172B)),
                        ),
                      ),
                    ),
                ],
              ),
            ),
          ),
          Divider(
            color: Color(0XFFDDDDDD),
            height: 30,
          ),
          Padding(
              padding: EdgeInsets.symmetric(horizontal: 15),
              child: Obx(() => Column(


                    children: [

                      if(announcementData.isEmpty)
                      isdata(),

                      for (var item in announcementData)
                        Column(
                          children: [
                            GestureDetector(
                              onTap: () => {
                                Get.toNamed(
                                    '/web_tabs/announcement/detail?uuid=${item['uuid']}')
                              },
                              child: Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      Container(
                                          width: 200,
                                          height: 50,
                                          child: Column(
                                            crossAxisAlignment:
                                                CrossAxisAlignment.start,
                                            children: [
                                              Row(
                                                crossAxisAlignment:
                                                    CrossAxisAlignment.start,
                                                children: [
                                                  Container(
                                                    padding:
                                                        EdgeInsets.symmetric(
                                                            horizontal: 5,
                                                            vertical: 2),
                                                    decoration: BoxDecoration(
                                                        color:
                                                            Color(0XFF0F172B),
                                                        borderRadius:
                                                            BorderRadius
                                                                .circular(3)),
                                                    child: Text(
                                                      '${item['category']['title']}',
                                                      style: TextStyle(
                                                          fontSize: 11,
                                                          color: Color(
                                                              0XFFFFFFFF)),
                                                    ),
                                                  ),
                                                  SizedBox(
                                                    width: 5,
                                                  ),
                                                  item['title'].length > 9
                                                      ? Text(
                                                          '${item['title'].substring(0, 9)}')
                                                      : Text(
                                                          '${item['title']}'),
                                                ],
                                              ),
                                              SizedBox(
                                                height: 2,
                                              ),
                                              item['title'].length > 9
                                                  ? Text(
                                                      "${item['title'].substring(9, item['title'].length)}",
                                                      overflow:
                                                          TextOverflow.ellipsis,
                                                      maxLines: 1)
                                                  : Text('')
                                            ],
                                          )),
                                      Text(
                                        '${item['created_at']}',
                                        style: TextStyle(
                                            fontSize: 11,
                                            color: Color(0XFF999999)),
                                      ),
                                    ],
                                  ),
                                  ClipRRect(
                                    borderRadius:
                                        BorderRadius.circular(7.0), // 设置圆角大小
                                    child: Image.network('${item['thumb']}',
                                        width: 105,
                                        height: 70,
                                        fit: BoxFit.fill, errorBuilder:
                                            (context, error, stackTrace) {
                                      return SizedBox(
                                        width: 105,
                                      );
                                    }),
                                  )
                                ],
                              ),
                            ),
                            SizedBox(
                              height: 15,
                            ),
                          ],
                        )
                    ],
                  )))
        ],
      ),
    );
  }
}
